﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace The_LarX
{
    #if !WINDOWS_PHONE
    public class LarVideoPlayer : Component
    {
        public Video Video;
        public VideoPlayer VideoPlayer;
        public bool Ended
        {
            get { return VideoPlayer.PlayPosition == Video.Duration; }
        }

        public override void DisableComponent()
        {
            base.DisableComponent();
            VideoPlayer.Dispose();
        }

        private string videoName;
        public string VideoName
        {
            get { return videoName; }
            set
            {
                videoName = value;
                Video = Engine.Content.Load<Video>(value);
                VideoPlayer = new VideoPlayer();
            }
        }

        public bool ResizeToScreen = false;
        public bool Queued = false;
        public int QueuedIndex = 0;

        public LarVideoPlayer(string video, GameScreen s)
            : base(s)
        {
            this.VideoName = video;
        }

        public void Play()
        {
            if (VideoPlayer != null)
                VideoPlayer.Play(Video);
        }

        public override void Draw()
        {
            if (VideoPlayer.State != MediaState.Stopped)
            {
                if (!Queued)
                {
                    DrawVideo();
                }
                else
                {
                    Engine.QueuedActions.AddQueuedAction(QueuedIndex, DrawVideo);
                }
            }
        }
        private void DrawVideo()
        {
            if (ResizeToScreen)
            {
                PresentationParameters pp = Engine.GraphicsDevice.PresentationParameters;
                Engine.RenderManager.Draw(VideoPlayer.GetTexture(), new Rectangle(0, 0, pp.BackBufferWidth,
                    pp.BackBufferHeight), Color.White, null);
            }
            else
            {
                Engine.RenderManager.Draw(VideoPlayer.GetTexture(), Vector2.Zero, null, false);
            }
        }
    }
#endif
}
